package com.rs.game.player.content;

import java.util.Random;
import java.util.TimerTask;

import com.rs.Settings;
import com.rs.cores.CoresManager;
import com.rs.game.World;
import com.rs.game.WorldTile;
import com.rs.game.player.Player;
import com.rs.utils.Logger;

public class TypeContest {

	private static String string [][] = {
		{"0zxdrf4teza", "0zxdrf4teza"},
		{"d5d8s6e2f", "d5d8s6e2f"},
		{"az95s943dfs", "az95s943dfs"},
		{"dbrntjtnw4h", "dbrntjtnw4h"}
	};
	
	public static int stringid = -1;
	public static int round = 1;
	public static boolean victory = false;
	
	public static void end() {
		for(Player participant : World.getPlayers()) {
			if(participant == null)
				continue;	
			participant.setNextWorldTile(new WorldTile(Settings.RESPAWN_PLAYER_LOCATION));
		}
	}
	
	public static void Start() {
		for(Player participant : World.getPlayers()) {
			participant.getPackets().sendGameMessage("<col=ff0000>[TypeContest] Welcome to the TypeContest!");
			participant.getPackets().sendGameMessage("<col=ff0000>[TypeContest] There are 10 rounds. In each round you will get a string of letters and numbers.");
			participant.getPackets().sendGameMessage("<col=ff0000>[TypeContest] The participant who is the first to correctly type the string, will be awarded with 1 CP and 100k.");
			participant.getPackets().sendGameMessage("<col=ff0000>[TypeContest] You can leave at any moment. The contest will start within 15 seconds!");
			participant.getPackets().sendGameMessage("<col=ff0000>[TypeContest] Good luck participants!");
			CoresManager.fastExecutor.schedule(new TimerTask() {
				@Override
				public void run() {
					try {
						TypeContest.Run();
					} catch (Throwable e) {
						Logger.handle(e);
					}
				}
			}, 0, 40000);
		}
	}

	
	public static int RandomQuestion() {
		int random = 0;
		Random rand = new Random();
		random = rand.nextInt(string.length);
		return random;
	}
	
	public static void Run() {
		int rand = RandomQuestion();
		stringid = rand;
		victory = false;
		for(Player participant : World.getPlayers()) {
			if(participant == null)
				continue;
			if(ContestArea(participant)) {
				participant.getPackets().sendGameMessage("<col=ff0000>[TypeContest] Round "+round+": "+string[rand][0]);
			}
		}
	}
	
	public static void verifyAnswer(final Player player, String answer) {
		if(victory) {
			player.getPackets().sendGameMessage("That round has already been won, wait for the next round.");
		} else if(string[stringid][1].equalsIgnoreCase(answer)) {
			sendRoundWinner(player.getDisplayName());
			player.getInventory().addItem(995, 100000);
			round++;
		} else if (!string[stringid][1].equalsIgnoreCase(answer)) {
			player.getPackets().sendGameMessage("That answer wasn't correct, please try it again.");
		} else {
			round++;
		}
	}
	
	public static void sendRoundWinner(String winner) {
		for(Player participant : World.getPlayers()) {
			if(participant == null)
				continue;
			if(ContestArea(participant)) {
				victory = true;
				participant.getPackets().sendGameMessage("<col=ff0000>[TypeContest]Congratulations, "+winner+" won round "+round+".");
			}
			if (round == 11) {
				TypeContest.end();
			}
		}
	}
	
	public static boolean ContestArea(final Player participant) {
		if(participant.getX() >= 2630 && participant.getX() <= 2660 && participant.getY() >= 9377 && participant.getY() <= 9400) {
			return true;
		}
		return false;
	}
}
